<template>
  <div class="my">
    <header>
      <div class="login" @click="goLogin" v-if="showLogin">登录/注册</div>
      <div class="userinfo" v-else>
        <img :src="this.userinfo.userimg" alt="" />
        <span>{{ this.userinfo.username }}</span>
      </div>
    </header>
    <section>
      <ul>
        <li @click="goAddress">地址管理</li>
        <li @click="removelogin">退出登录</li>
      </ul>
    </section>
    <Tabbar></Tabbar>
  </div>
</template>

<script>
import Tabbar from "../../components/common/Tabbar.vue";
import qs from "qs";
export default {
  name: "My",
  components: {
    Tabbar,
  },
  data() {
    return {
      showLogin: false,
      token: {
        token: "",
      },
      userinfo: "",
    };
  },
  created() {
    this.getUser();
  },
  methods: {
    //跳转登录页面
    goLogin() {
      this.$router.push("/login");
    },
    //获取当前用户的信息
    getUser() {
      this.token.token = window.sessionStorage.getItem("token");
      // console.log(this.token.token);
      if (this.token.token == null) return (this.showLogin = true);
      this.$http.post("/getUserinfo", qs.stringify(this.token)).then((res) => {
        // console.log(res.data);
        this.userinfo = res.data;
        console.log(this.userinfo);
      });
    },
    //地址管理页面
    goAddress() {
      this.$router.push("/address/addressIndex");
    },
    //退出登录
    removelogin() {
      window.sessionStorage.removeItem("token");
      this.showLogin = true;
    },
  },
};
</script>

<style lang="less" scoped>
.my {
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}
header {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100px;
  background-color: #b0353f;
  .login {
    background-color: #f6ab32;
    color: #fff;
    width: 75px;
    font-size: 16px;
    border-radius: 6px;
    padding: 6px 15px;
  }
  .userinfo {
    display: flex;
    flex-direction: column;
    justify-items: center;
    align-items: center;
    img {
      width: 50px;
      border-radius: 50%;
    }
    span {
      padding-top: 5px;
      font-size: 14px;
    }
  }
}

section {
  flex: 1;
  overflow: hidden;
  ul li {
    padding: 10px;
    font-size: 16px;
  }
}
</style>